# Maintainer: Maksim Bondarenkov <maksapple2306@gmail.com>

_realname=wiiuse
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.15.6
pkgrel=1
pkgdesc="Feature complete cross-platform Wii Remote access library (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url='https://github.com/wiiuse/wiiuse'
license=('spdx:GPL-3.0-or-later')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-doxygen")
source=("https://github.com/wiiuse/wiiuse/archive/${pkgver}/${_realname}-${pkgver}.tar.gz"
        "0001-fix-cmakelists.patch")
sha256sums=('a3babe5eb284606090af706b356f1a0476123598f680094b1799670ec1780a44'
            '116204ffbdfb83e9a074e74980bdfe7fed0e1b5ead95209ca6e536851f7c77bc')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  patch -Np1 -i "${srcdir}/0001-fix-cmakelists.patch"
}

build() {
  declare -a _cmake_opts=(
    -GNinja
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}"
    -DBUILD_EXAMPLE=OFF
    -DBUILD_EXAMPLE_SDL=OFF
    -DINSTALL_EXAMPLES=OFF
  )

  if check_option "debug" "n"; then
    _cmake_opts+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _cmake_opts+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  mkdir -p "${srcdir}/build-${MSYSTEM}-static" && cd "${srcdir}/build-${MSYSTEM}-static"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      "${_cmake_opts[@]}" \
      -DBUILD_SHARED_LIBS=OFF \
      ../${_realname}-${pkgver}

  "${MINGW_PREFIX}"/bin/cmake.exe --build .

  mkdir -p "${srcdir}/build-${MSYSTEM}-shared" && cd "${srcdir}/build-${MSYSTEM}-shared"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      "${_cmake_opts[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      ../${_realname}-${pkgver}

  "${MINGW_PREFIX}"/bin/cmake.exe --build .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}-static"
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .

  cd "${srcdir}/build-${MSYSTEM}-shared"
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
